package comp512.classes;

import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.TreeSet;

@SuppressWarnings("serial")
public class Question implements Serializable {
	// private static final long serialVersionUID = 1L;
	private String question;
	private TreeSet<Answer> answers;
	private HashMap<String, Answer> answerMap;
	private HashSet<String> answerSet;

	public Question() {

	}

	public Question(String question) {
		this.question = question;
		answers = new TreeSet<>();
		answerSet = new HashSet<>();
		answerMap = new HashMap<>();
	}

	public String getQuestion() {
		return question;
	}

	public void addAnswer(Answer newAnswer) {
		answers.add(newAnswer);
		answerSet.add(newAnswer.getAnswer().toLowerCase());
		answerMap.put(newAnswer.getAnswer().toLowerCase(), newAnswer);
	}

	public TreeSet<Answer> getAnswers() {
		return answers;
	}

	public Answer getBestAnswer() {
		return answers.last();
	}

	public int getAnswerCount() {
		return answers.size();
	}

	public boolean validateAnswer(String answerCheck) {
		if (answerSet.contains(answerCheck.toLowerCase()))
			return true;
		else
			return false;
	}

	public Answer getAnswer(int position) {
		if (position < answers.size()) {
			Object[] ans = answers.toArray();
			Answer rtn = (Answer) ans[answers.size() - position - 1];
			rtn.setAnswer(Character.toUpperCase(rtn.getAnswer().charAt(0))
					+ rtn.getAnswer().substring(1));
			return rtn;
		}
		return null;
	}
	
	public Answer getCheckedAnswer(String ansEntered){
		if(answerMap.containsKey(ansEntered.toLowerCase()))
			return answerMap.get(ansEntered.toLowerCase());
		else
			return null;
	}
}
